#include "Etu.h"
#include <iostream>
#include <fstream>

using namespace std;

Etu::Etu(std::string ip, int port):Agent(ip,port)
{
  AstPriority=false;
  repondu = true;
  pthread_cond_init(&CL, NULL);
  pthread_cond_init(&CE, NULL);
  etat="";
}

Etu::~Etu()
{
  sendProto("ETUD/LOGOUT");
}

void Etu::start()
{
	vector<string> rooms;
	
	int nbSalle = 0;
	string login;string buf;int lsize=0;char c;
	cout<<"Votre LOGIN : ";
  	getline(cin, login);
	sendProto("ETUD/LOGIN");
	lsize =login.size();
	sendInt(lsize);
	sendString(login.c_str(),lsize);
	buf = getProto();
	if(buf!="ETUD/LISTE")
	    return;
	else
	{		
	  cout<<"Choix de la salle"<<endl;
	  int tailleNomSalle;
	  nbSalle = getInt();
	  if(nbSalle == 0)
	  {
		  cout << "Aucune salle n'est disponible" << endl;
		  return;
	  }
	  for(int i=0;i<nbSalle;i++)
	  {
		  tailleNomSalle=getInt();
		  buf=getString(tailleNomSalle);
		  rooms.push_back(buf);
		  cout<<"      " << buf<<endl;		  
	  }
	  bool checkroomname = false;
	  if(rooms.size() == 0)
		return ;
	  do{
		  cout<<"Quelle salle voulez vous?"<<endl;
		  getline(cin, buf);
		  for(int j=0; j<nbSalle; j++)
		  {
			  if(buf == rooms[j])
				checkroomname = true;
		  }
		}while(checkroomname != true);
		
		sendProto("ETUD/CHOIX/SALLE");
		sendInt(buf.size());
		sendString(buf.c_str(),buf.size());
		string roomname = buf;
		buf=getProto();
		
		if(buf=="ETUD/ERR/CHOIXSALLE")
		{
			cout<<"Erreur"<<endl;
			return ;
		}
		
		if(buf=="ETUD/OK/CHOIXSALLE")
		{
			cout<<"Vous etes dans la salle : "<<roomname<<endl<<endl;
			cout<<"Choix des activites"<<endl;
			int nbActivites=0;
			int tailleNomActivites;
			nbActivites=getInt();
			for(int j =0;j<nbActivites;j++){
				tailleNomActivites=getInt();
				buf=getString(tailleNomActivites);
				cout <<"      " << buf << endl;
			}
			
			cout<<"Quelle activite voulez vous faire ?"<<endl;
			getline(cin, buf);
			sendProto("ETUD/CHOIX/ACTIVITE");
			sendInt(buf.size());
			sendString(buf.c_str(),buf.size());
			buf=getProto();
				
			if (buf == "ETUD/ERR/CHOIXACTIVITE")			
				return ;
				
			cout<<"choix validé !"<<endl;
		}		
		/* l'Etu est maintenant connecté */
		pthread_create(&ecrire,NULL,gereEcriture,(void *)this);
		pthread_create(&lire,NULL,gereLecture,(void *)this);
		pthread_join(lire,NULL);
		pthread_join(ecrire,NULL);		  
	}
}

void * Etu::gereLecture(void * arg)
{
	Etu * param = (Etu *)arg;
	string buf;
	string msg;
	int tailleMSG;

	int iter=0,tQ=0;string Q; int tLR;
	ofstream of("tempVOIP.wav", ios::out| ios::trunc);
	ofstream ofVOIP;
	while (1)
	{
		////////// section critique ////////////
		pthread_mutex_lock(&param->v);
		if(!param->repondu)
			pthread_cond_wait(&param->CL, &param->v);
			
		buf = param->getProto();

		param->etat = buf;
		//////////////////// debut des comparaisons /////////////////////
		if(buf == "AST/MSG")
		{
			// on lit un int 
			tailleMSG=param->getInt();
			msg = param->getString(tailleMSG);
			cout << msg << endl;
		}                  
		if(buf == "AST/VOIP")
		{
			/* _ _ _  */ tQ=param->getInt();
			Q=param->getString(tQ);
			ofVOIP.open("tempVOIP.wav",ios::out| ios::trunc);
			ofVOIP<<Q<<endl;
			ofVOIP.close();
			system("... tempVOIP.wav");//demander commande a MR MARTIN
		}                                    
		if(buf == "ETU/QCM")
		{
			/* 
			* on lit la taille de la question pis la question
			* puis la taille de la liste de reponse
			* puis on lit une a une les reponses
			* soit la taille de la réponse pusi la reponse 
			*/
			tQ=param->getInt();
			Q=param->getString(tQ);
			cout<<Q<<endl;
			tLR=param->getInt();
			
			for (iter =0 ;iter<tLR;iter++)
			{
				tQ=param->getInt();
				Q=param->getString(tQ);
				cout<<Q<<endl;
			}                 
		} 

		if(buf == "ETU/Q2")
		{
			tQ=param->getInt();
			Q=param->getString(tQ);
			cout<<Q<<endl;
		}

		if(buf == "ETU/Q3")
		{
			tQ=param->getInt();
			Q=param->getString(tQ);
			of.open("temp.wav",ios::out| ios::trunc);
			of<<Q<<endl;
			of.close();
			system("aplay temp.wav");//demander commande a MR MARTIN
		}                   
		if(buf == "ETU/Q4")
		{
			tQ=param->getInt();
			Q=param->getString(tQ);
			of.open("temp.wav",ios::out| ios::trunc);
			of<<Q<<endl;
			of.close();
			system("... temp.wav");//demander commande a MR MARTIN
		}                         
		///if(buf == "ET U/OBS /Q4"){}                   
		if(buf == "ETU/FIN/ACTIVITE")
			return NULL; //on kick l'etudiant          

		pthread_cond_signal(&param->CE);
		param->repondu=false;
		pthread_mutex_unlock(&param->v); 
		////////// fin section critique && fin boucle ////////
	}
	return NULL;

}

void * Etu::gereEcriture(void * arg)
{
	Etu * param = (Etu *)arg;
	string buf;
	bool continu=true;
	int repCount=0;
	vector<string> Vrep;
	vector<int> Vtrep;
	int iter=0;
	while (1)
	{
		////////// section critique ////////////
		pthread_mutex_lock(&param->v);
		if(param->repondu || param->etat == "unknown")
			pthread_cond_wait(&param->CE, &param->v);
		//////////////////// debut des comparaisons /////////////////////
		if(param->etat == "AST/MSG")
		{
			/* 
			* il a reçu un msg d'un ast 
			* il doit répondre
			*/
			cout << "A l'assistant :";
			getline(cin, buf);
			param->sendProto("ETUD/AST/MSG");
			param->sendInt(buf.size());
			param->sendString(buf.c_str(), buf.size());
			param->etat = "unknown";
		}                  
		else if(param->etat == "AST/VOIP")
		{
			/*
			* ignoré pour le moment
			*/
		}                                    
		else if(param->etat == "ETU/QCM")
		{
			cout << "Votre réponse : ";
			getline(cin,buf);
			/* on envoi la taille de Vrep */
			param->sendInt(buf.size());
			param->sendString(buf.c_str(), buf.size());
			param->etat = "unknown";
		}
		else if(param->etat == "ETU/Q2")
		{
			cout<<"Votre réponse :";
			getline(cin,buf);
			param->sendInt(buf.size());
			param->sendString(buf.c_str(),buf.size());
		}
		else if(param->etat == "ETU/Q3")
			{
			cout<<"Votre réponse :";
			getline(cin,buf);
			param->sendInt(buf.size());
			param->sendString(buf.c_str(),buf.size());
		}                   
		else if(param->etat == "ETU/Q4")
		{
			/*
			* reponse VOIP ignorée pour le moment 
			*/ 
		}                         

		pthread_cond_signal(&param->CL);
		param->repondu=true;
		pthread_mutex_unlock(&param->v); 
		////////// fin section critique && fin boucle ////////

		}
		return NULL;
	}
